<template>
  <div class="register-container">
    <div class="register-form">
      <h2>用户注册</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input type="text" id="username" v-model="registerForm.username" required />
        </div>
        
        <div class="form-group">
          <label for="password">密码:</label>
          <input type="password" id="password" v-model="registerForm.password" required />
        </div>
        
        <div class="form-group">
          <label for="fullName">真实姓名:</label>
          <input type="text" id="fullName" v-model="registerForm.fullName" required />
        </div>
        
        <div class="form-group">
          <label for="userType">用户类型:</label>
          <select id="userType" v-model="registerForm.userType" @change="onUserTypeChange" required>
            <option value="1">教师</option>
            <option value="2">学生</option>
            <option value="3">家长</option>
          </select>
        </div>
        
        <div class="form-group" v-if="registerForm.userType === '2'">
          <label for="studentNumber">学号:</label>
          <input type="text" id="studentNumber" v-model="registerForm.studentNumber" required />
        </div>
        
        <button type="submit" class="register-btn">注册</button>
      </form>
      
      <div class="login-link">
        <p>已有账号? <router-link to="/login">立即登录</router-link></p>
      </div>
      
      <div v-if="error" class="error-message">
        {{ error }}
      </div>
      
      <div v-if="success" class="success-message">
        {{ success }}
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router';

export default {
  name: 'Register',
  setup() {
    const router = useRouter();
    const error = ref('');
    const success = ref('');
    
    const registerForm = ref({
      username: '',
      password: '',
      fullName: '',
      userType: '2', // 默认学生
      studentNumber: ''
    });
    
    const onUserTypeChange = () => {
      // 当用户类型改变时，清空学号
      if (registerForm.value.userType !== '2') {
        registerForm.value.studentNumber = '';
      }
    };
    
    const handleRegister = async () => {
      try {
        // 如果是学生注册，必须填写学号
        if (registerForm.value.userType === '2' && !registerForm.value.studentNumber) {
          error.value = '学生注册必须填写学号';
          return;
        }
        
        // 如果不是学生注册，清除学号字段
        const requestData = { ...registerForm.value };
        if (registerForm.value.userType !== '2') {
          delete requestData.studentNumber;
        }
        
        const response = await fetch('/user/register', {
          method: 'POST',
          credentials: 'include',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify(requestData)
        });
        
        const result = await response.json();
        if (result.code === 1) {
          success.value = result.msg || '注册成功';
          error.value = '';
          
          // 注册成功后2秒跳转到登录页
          setTimeout(() => {
            router.push('/login');
          }, 2000);
        } else {
          error.value = result.msg || '注册失败';
          success.value = '';
        }
      } catch (err) {
        error.value = '网络错误，请稍后重试';
        success.value = '';
      }
    };
    
    return {
      registerForm,
      error,
      success,
      onUserTypeChange,
      handleRegister
    };
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20px;
}

.register-form {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.register-form h2 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: bold;
  color: #555;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  box-sizing: border-box;
}

.register-btn {
  width: 100%;
  padding: 0.75rem;
  background-color: #67C23A;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  margin-top: 1rem;
}

.register-btn:hover {
  background-color: #55a52a;
}

.login-link {
  text-align: center;
  margin-top: 1rem;
}

.login-link a {
  color: #409EFF;
  text-decoration: none;
}

.login-link a:hover {
  text-decoration: underline;
}

.error-message {
  background-color: #f8d7da;
  color: #721c24;
  padding: 0.75rem;
  border-radius: 4px;
  margin-top: 1rem;
}

.success-message {
  background-color: #d4edda;
  color: #155724;
  padding: 0.75rem;
  border-radius: 4px;
  margin-top: 1rem;
}
</style>